﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KCL.Code.Entities;
using KCL.Code.Business;

namespace KCL.Management
{
    /// <summary>
    /// Summary description for PhaseManagement
    /// </summary>
    public class PhaseManagement : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            // get parameters from query string
            var PhaseCode = context.Request.Form["PhaseCode"].Trim();
            var PhaseName = context.Request.Form["PhaseName"].Trim();            
            var Price = context.Request.Form["Price"].Trim();           
            var action = context.Request.Form["action"];

            context.Response.ContentType = "application/html";
            var responseData = string.Empty;

            // return empty if question or keyword is missing
            if (string.IsNullOrEmpty(PhaseName) || string.IsNullOrEmpty(Price))
            {
                context.Response.Write("alert(\"Thông tin tên công đoạn, giá thành không hợp lệ!\");");
                return;
            }

            Phase phase = new Phase();

			//phase.id = Convert.ToInt32(PhaseCode);
            phase.name = PhaseName;
            phase.price = Convert.ToInt32(Price);

            try
            {
                if (action == "create")
                    PhaseServices.CreatePhase(phase);
                else if (action == "edit")
                    PhaseServices.UpdatePhase(phase);
            }
            catch (Exception ex)
            {
                context.Response.Write(string.Format("alert(\"Có lỗi: {0}\");", ex.Message));
                return;
            }

            if (action == "create")
				context.Response.Write("$('#divCreatePhase').dialog('close'); alert('Thêm mới thành công.'); self.location='PhaseManagement.aspx';");
            else if (action == "edit")
				context.Response.Write(string.Format("$('#divCreatePhase').dialog('close'); alert('Chỉnh sửa thành công.'); RefreshCustomer({0});", 1));
            else if (action == "delete")
                context.Response.Write("alert('Xóa thành công.');");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}